<?php
    // Error Reporting

    error_reporting(E_ALL);
    ini_set("display_errors", "on");

    // header('Content-type: image/gif');

    include('./lzw.php');

    $addToFlatfile = true;
    if (isset($_GET["e"])) {
        $userPath = "./data/flatfiles/".$_GET["u"];
        if (!is_dir($userPath)) {
            mkdir($userPath);
        }
        $filename = $_GET["r"].".txt";
        foreach($_GET as $key => $value) {
            if ($key != "e") {
                $params = urldecode($key);
            }
        }
        $addToFlatfile = false;
        $flatHandler = fopen($userPath."/".$filename, "a");
        fwrite($flatHandler, $params);
        fclose($flatHandler);
        if ($_GET["e"] == "0") {
            $addToFlatfile = true;
            $flatHandler = fopen($userPath."/".$filename, "r");
            $params = decompress(base64_decode(fgets($flatHandler)));
            fclose($flatHandler);
            unlink($userPath."/".$filename);
        }
        touch($userPath);
    }
    if ($addToFlatfile) {
        if (!isset($_GET["e"])) {
            foreach($_GET as $key => $value) {
                $params = decompress(base64_decode(urldecode($key)));
                break;
            }
        }
        $params = explode("&", $params);
        $finalParams = array();
        foreach($params as $param) {
            $paramArr = explode("=", $param);
            $finalParams[$paramArr[0]] = $paramArr[1];
        }
        $live = htmlentities($finalParams['live']);
        $livefeed = htmlentities($finalParams['livefeed']);
        $loadtime = htmlentities($finalParams['loadtime']);
        $timestamp = htmlentities($finalParams['timestamp']);
        $trk_id = htmlentities($finalParams['trk_id']);
        $user_id = htmlentities($finalParams['user_id']);
        $plugins = htmlentities($finalParams['plugins']);
        $data = json_decode(stripslashes($finalParams['json']));

        $userPath = "./data/flatfiles/".$user_id;
        if (!is_dir($userPath)) {
            mkdir($userPath);
        }
        $filename = $timestamp.".txt";
        $entry = "";
        $firstlive = false;
        if ($live != "") {
            $liveData = '{"customer_id":"'.$trk_id.'","user_id":"'.$user_id.'","elapsed":"'.$loadtime.'"';
        }
        if (!file_exists($userPath."/".$filename)) {
            $entry .= "start::".time()."\n";
        }
        $flatHandler = fopen($userPath."/".$filename, "a");
        if (isset($plugins) && $plugins != null && $plugins != "") {
            fwrite($flatHandler, "plugins::".$plugins."\n");
        }
        if ($data != null) {
            foreach($data as $key => $value) {
                if (is_array($value)) {
                    foreach ($value as $arrValue) {
                        if ($live != "" && (substr($arrValue, 0, 3) == "i::" || substr($arrValue, 0, 3) == "o::" || substr($arrValue, 0, 3) == "s::")) {
                            $liveData .= ',"'.substr($arrValue, 0, 1).'":"'.substr($arrValue, 3).'"';
                        }
                        $entry .= $arrValue."\n";
                    }
                }
                else if (is_object($data)){
                    if ($key == "trk_id") {
                        $firstlive = true;
                    }
                    $entry .= $key."::".$value."\n";
                    if ($live != "" && ($key == "browser" ||$key == "country" || $key == "path" || $key == "referrer")) {
                        $liveData .= ',"'.$key.'":"'.$value.'"';
                    }
                }
                else {
                    $entry .= $key."::".$value."\n";
                }
            }
        }
        if ($entry != "") {
            fwrite($flatHandler, $entry);
            fclose($flatHandler);
            touch($userPath);
        } else {
            fclose($flatHandler);
        }
        $doLiveFeed = false;
        if ($live != "") {
            if ($firstlive) {
                $liveData .= ',"start":"'.time().'"';
            }
            $liveData = "live=".$live."&data=".$liveData."}";
            if ($livefeed == "true") {
                $liveData .= "&positions=".$finalParams['json'];
            }
            /*$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'http://gurami.trakken.info/trakksuite/php/redis/liveSession.php');
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $liveData);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $doLiveFeed = (bool)curl_exec($ch);
            curl_close($ch);*/
        }
        if ($doLiveFeed) {
            // include('img/tm_pixel_live.gif');
        } else {
            // include('img/tm_pixel.gif');
        }
    } else {
        // include('img/tm_pixel.gif');
    }
?>
